import {createSlice} from '@reduxjs/toolkit'
import { getUserListApi } from '../../apis/user'
import { delAccountApi } from '../../apis/user'

const usersSlice = createSlice({
    name: 'users',
    initialState: {
        data: [],
        total: 0,
        loading: false
    },
    reducers: {
        setLoading(state, action) {
            state.loading = action.payload
        },

        setDataAndTotal(state, action) {
            state.data = action.payload.data,
            state.total = action.payload.total
        }
    }
})

export const { setLoading, setDataAndTotal } = usersSlice.actions
export default usersSlice.reducer

export const getData = (params = { pageSize: 10, pageNumber: 1 }) => async (dispatch) => {
    dispatch(setLoading(true))
    const res = await getUserListApi(params)
    console.log('用户信息', res);
    dispatch(setDataAndTotal({ data: res.data, total: res.totalCount }))
    // setTotal(res.totalCount)
    dispatch(setLoading(false))

}

export const delData = (_id) => async (dispatch) => {
    await delAccountApi(_id)
    dispatch(getData())
}